﻿using DryWCS.Model.GlobalPara;
using DryWCS.Service.GlobalVariable;
using DryWCS.Service.User;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace DryWCS.App.ViewModel
{
    public class LoginViewModel : ViewModelBase
    {
        #region 用户名/密码

        /// <summary>
        /// 数据库访问类型
        /// </summary>
        public string ServerType { get; set; }

        /// <summary>
        /// 皮肤样式
        /// </summary>
        public string SkinName { get; set; }

        private string report;


        //报错信息
        public string Report
        {
            get { return report; }
            set
            {
                report = value;
                this.RaisePropertyChanged("Report");
            }
        }

        /// <summary>
        /// 用户名
        /// </summary>
        public string Nikname { get; set; }


        /// <summary>
        /// 记住密码
        /// </summary>
        public bool IsRemember { get; set; } = true;


        /// <summary>
        /// 密码
        /// </summary>
        public string Password { get; set; }


        /// <summary>
        /// 禁用按钮
        /// </summary>
        public bool IsCancel { get; set; } = true;

        public string HelpQq { get; set; }
        public string HelpEmail { get; set; }
        public string HelpWechat { get; set; }
        UserService userService = new UserService();
        #endregion

        #region 构造函数
        public LoginViewModel()
        {
            ReadConfigInfo();//从配置文件读取数据
            CloseCommand = new RelayCommand<Window>(t => t.Close());
            LoginCommand = new RelayCommand<Window>(t => Login(t));
            QqCommand = new RelayCommand(QqchartStart);
            EmialCommand = new RelayCommand(EmailchartStart);
        }
        #endregion

        #region 命令(Binding Command)
        public RelayCommand<Window> CloseCommand { get; set; }

        public RelayCommand<Window> LoginCommand { get; set; }

        public RelayCommand QqCommand { get; set; }

        public RelayCommand EmialCommand { get; set; }


        private RelayCommand _exitCommand;

        public RelayCommand ExitCommand
        {
            get
            {
                if (_exitCommand == null)
                {
                    _exitCommand = new RelayCommand(() => ApplicationShutdown());
                }
                return _exitCommand;
            }
        }

        #endregion

        #region  登录方法实现
        private void Login(Window window)
        {
            Check();//先检查是否为空
            GlobalPara.Loginer = userService.GetUserModelByPassword(Nikname, Password);
            if (GlobalPara.Loginer != null)
            {
                window.DialogResult = true;
                if(IsRemember)
                {
                    GV.ChangeConfig("IsRemember", "1");
                    GV.ChangeConfig("LastLoginer",Nikname);
                    GV.ChangeConfig("LastLoginPw", Password);
                }
                else
                    GV.ChangeConfig("IsRemember", "0");
            }
            else
            {
                Report = "用户名或者密码错误";
            }

        }
        private bool Check()
        {
            bool ret = false;
            if (string.IsNullOrWhiteSpace(Nikname))
            {
                Report = "用户名不能为空";
                return ret;
            }
            if (string.IsNullOrWhiteSpace(Password))
            {
                Report = "密码不能为空";
                return ret;
            }
            return true;
        }


        #endregion

        #region Qq 启动会话
        private void QqchartStart()
        {
            Process.Start($"http://wpa.qq.com/msgrd?v=3&uin={HelpQq}&site=qq&menu=yes");
        }
        #endregion

        #region Emial 启动会话
        private void EmailchartStart()
        {
            Process.Start($"mailto://{HelpEmail}?cc=HelpEmail&subject=这是邮件标题Title&body=这是邮件内容Body");
        }
        #endregion

        #region 关闭系统
        /// <summary>
        /// 关闭系统
        /// </summary>
        public void ApplicationShutdown()
        {
            Messenger.Default.Send("", "ApplicationShutdown");
        }

        #endregion

        #region Read DB config info
        void ReadConfigInfo()
        {
            IsRemember = GV.IsRemember;
            HelpQq = GV.HelpQq;
            HelpEmail = GV.HelpEmail;
            HelpWechat = GV.HelpWechat;
            if (IsRemember)
            {
                Nikname = GV.GetConfigValue("LastLoginer");
                Password = GV.GetConfigValue("LastloginPw");
            }
        }
        #endregion

    }
}
